﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;

namespace SaaS
{
    /// <summary>
    /// Summary description for ObjectUpdate
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ObjectUpdate : System.Web.Services.WebService
    {

        [WebMethod]
        public string insert(string xmlStr,string nameStr,string tyepStr,string nullStr,string defaultStr,string priStr)
        {
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(xmlStr);

            XmlNodeList xnl = xd.SelectNodes("//col/columnID");
            int maxID = 0;
            foreach (XmlNode xn in xnl)
            {
                int id = int.Parse(xn.InnerText.ToString());
                if (id > maxID)
                    maxID = id;
            } 

            XmlNode xnd = xd.SelectSingleNode("columns");
            XmlElement xe_u = xd.CreateElement("col");
            XmlElement xe_a1 = xd.CreateElement("name");
            xe_a1.InnerText = nameStr;
            xe_u.AppendChild(xe_a1);
            XmlElement xe_a2 = xd.CreateElement("type");
            //xe_a2.InnerText = txtType.Text.Trim() ;
            xe_a2.InnerText = tyepStr;
            xe_u.AppendChild(xe_a2);
            XmlElement xe_a3 = xd.CreateElement("allownull");
            xe_a3.InnerText = nullStr;
            xe_u.AppendChild(xe_a3);
            XmlElement xe_a4 = xd.CreateElement("default");
            xe_a4.InnerText = defaultStr;
            xe_u.AppendChild(xe_a4);
            XmlElement xe_a5 = xd.CreateElement("columnID");
            xe_a5.InnerText = (maxID + 1).ToString();
            xe_u.AppendChild(xe_a5);
            XmlElement xe_a6 = xd.CreateElement("priKey");
            xe_a6.InnerText = priStr;
            xe_u.AppendChild(xe_a6);

            xnd.AppendChild(xe_u);

            System.IO.StringWriter sw = new System.IO.StringWriter();
            XmlTextWriter xw = new XmlTextWriter(sw);
            xd.WriteTo(xw);
            string newXmlStr = sw.ToString();

            return newXmlStr;
        }
    }
}
